package com.Beans.Dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.Beans.Models.Area;
import com.Beans.Models.Recursoinformatico;
import com.Beans.Dao.HibernateUtil;

public class RecursoinformaticoDAO {
	
	public static Recursoinformatico findById(int id){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tr = null;
		Recursoinformatico ri = null;
		try{
			tr = session.beginTransaction();
			Query q = session.createQuery("FROM recursoinformatico ri WHERE ri.id = :id");
			q.setParameter("id", id);
			ri = (Recursoinformatico)q.uniqueResult();
			tr.commit();
		}catch(HibernateException e){
			tr.rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
		return ri;
	}
	
	public static List<Recursoinformatico> findAll(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tr = null;
		List <Recursoinformatico> ri = null;
		try{
			tr = session.beginTransaction();
			Query q = session.createQuery("FROM recursoinformatico");
			ri = (List<Recursoinformatico>)q.list();
			tr.commit();
		}catch(HibernateException e){
			tr.rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
		return ri;	
	}
	
	public static List<Recursoinformatico> findByArea(Area area){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tr = null;
		List <Recursoinformatico> ri = null;
		try{
			tr = session.beginTransaction();
			Query q = session.createQuery("FROM Recursoinformatico ri WHERE ri.area = :area");
			q.setParameter("area", area);
			ri = (List<Recursoinformatico>)q.list();
			tr.commit();
		}catch(HibernateException e){
			tr.rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
		return ri;
	}
}
